#ifndef RCSESSION_H
#define RCSESSION_H

#include <QObject>

#include "settings.h"

// from WccApi
namespace WccNameSpace{
    namespace Internal {
        class WccNetworkAccessManager;
    }
}
namespace WccApi = WccNameSpace::Internal;

// fwd decl of interface for user interaction
class GeneratorOutputView;


/**
  * RcSession represents a remote compiler session. Authentication
  * and further requests to the service are done through this instance.
  */
class RcSession : public QObject
{
    Q_OBJECT

private:
    //
    // private members
    //
    GeneratorOutputView       & m_outputView;
    WccApi::WccNetworkAccessManager   * m_networkAccessManager;

    QString                     m_forumNokiaUserName;

    Settings::ProxyConfigKind   m_proxyConfig;
    QString                     m_proxyUrl;
    quint16                     m_proxyPort;
    QString                     m_proxyUserName;
    QString                     m_proxyPassword;

    QByteArray                  m_sessionIdCookie;
    int                         m_currentReqId;

public:
    //
    // public operators
    //

    /**
      * Construct an RcSession loading the settings.
      * It will be in an non-connected,
      * unauthorized state.
      */
    RcSession(GeneratorOutputView & outputView,
              QObject             * parent);
    ~RcSession();

    /**
      * @returns if this session is signed in to the remote compiler service.
      */
    bool isSignedOn() const;

    /**
      * Tries to sign in, if not signed in yet. May invoke methods on ui (ctor arg)
      * if necessary (such as proxy settings, authentication details and
      * password).
      */
    void signIn();

    /**
      * Signs off from service, loses state.
      */
    void signOut();

    /**
      * Loads the available targets from the service, and persists it to settings
      * (without parsing it, as it has been recevied).
      */
    void refreshRcProperties();

    /**
      * Creates a request URL, starting with the address of the service,
      * and ending with the request arguments (operation).
      *
      * @param operation the request arguments / operations to stick at the
      *        end of the request URL created.
      */
    static QString makeRequestUrl(const QString & operation);


    /**
      * @returns the network access manager instance used.
      */
    WccApi::WccNetworkAccessManager * networkAccessManager();


    /**
      * @returns the next request id (each invocation increases).
      */
    int nextReqId();


signals:

    /**
      * Emitted when signing process is done.
      *
      * @param success - whether signing in succeeded or failed.
      */
    void signInCompleted(bool success);


    /**
      * Emitted when RcProperties requesting has completed.
      *
      * @param success - whether request was successful
      *
      * @param hasRcPropertiesChanged - if the request was successful
      *        and the persisted value of RcProperties have changed
      *        this is also set to true.
      */
    void refreshRcPropertiesCompleted(bool success,
                                      bool hasRcPropertiesChanged);

private slots:
    void ssoFormReplySlot();
    void ssoAuthReplySlot();
    void ssoTicketSendingFinishedSlot();
    void propertiesReplySlot();

private:
    //
    // implementation details
    //
    void loadSettings();
    void setSessionIdCookie(const QByteArray & sessionIdCookie);
};

#endif // RCSESSION_H
